package org.misalen.web.server.config;

/**
 * server自定义响应结构
 */
public class ServerResult {

	private int errorCode;

	private String message;

	private Object data;

	public int getErrorCode() {
		return errorCode;
	}

	public String getMessage() {
		return message;
	}

	public Object getData() {
		return data;
	}

	private ServerResult() {
	}

	private ServerResult(ErrorCode code, String msg, Object data) {
		this.errorCode = code.value;
		if (msg == null) {
			this.message = code.message;
		} else {
			this.message = msg;
		}

		this.data = data;
	}

	/**
	 * 错误
	 * 
	 * @param code
	 * @return
	 */
	public static ServerResult error(ErrorCode code) {
		return new ServerResult(code, null, null);
	}

	/**
	 * 错误
	 * 
	 * @param code
	 * @param data
	 * @return
	 */
	public static ServerResult error(ErrorCode code, String msg) {
		return new ServerResult(code, msg, null);
	}

	/**
	 * 成功
	 * 
	 * @param data
	 * @return
	 */
	public static ServerResult ok(Object data) {
		return new ServerResult(ErrorCode.ERROR_CODE_SUCCESS, null, data);
	}

	/**
	 * 成功
	 * 
	 * @return
	 */
	public static ServerResult ok() {
		return new ServerResult(ErrorCode.ERROR_CODE_SUCCESS, null, null);
	}

}
